<?php


namespace App\Http\Controllers\Mini;

use App\Http\Controllers\Controller;
use App\Services\AddressService;
use Illuminate\Http\Request;

/**
 * 地址接口
 * Class AddressController
 * @package App\Http\Controllers\Mini
 */
class AddressController extends Controller
{

    public $addressModel;

    public function __construct()
    {
        $this->addressModel = new AddressService();
    }


    /**
     * @param Request $request
     * 说明用户必须在登录态
     * @address_id => 地址id
     * @is_default => false || true   是否获取默认地址
     */
    public function getAddress(Request $request)
    {
        try {
            if ($request->is_default == true && $request->is_default !== null) {
                return $this->addressModel->getAddresses(false, false, true);
            }
            if ($request->address_id) {
                return $this->addressModel->getAddresses($request->address_id);
            }
            if ($request->is_default == false && $request->address_id == false) {
                return $this->addressModel->getAddresses();
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 添加地址
     * @return array
     */
    public function addAddress()
    {
        return $this->addressModel->add();
    }


    /**
     * 修改地址
     * @param Request $request
     */
    public function editAddress(Request $request)
    {
        if ($request->address_id) {
            return $this->addressModel->edit($request->address_id);
        }
    }

    /**
     * 删除地址
     * @param Request $request
     */
    public function delAddress(Request $request)
    {
        if ($request->address_id) {
            return $this->addressModel->del($request->address_id);
        }
    }

    /**
     * 设置默认地址
     * @param Request $request 请求信息
     * @address_id   地址id
     */
    public function getSetDefault(Request $request)
    {
        if ($request->address_id) {
            return $this->addressModel->setDefault($request->address_id);
        }
    }

    public function __destruct()
    {
        // TODO: Implement __destruct() method.
        $this->addressModel;
    }


}
